#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

/*void backtrack(vector<int>& n, int start = 0)
{
    if(start == n.size())
    {
        for(int e : n) cout << e << ' ';
        cout << endl;
        return;
    }

    for(int i = start; i < n.size(); i++)
    {
        swap(n[start], n[i]);
        backtrack(n, start + 1);
        swap(n[start], n[i]);
    }
} */

void backtrack(vector<int>& n, int start = 0) {
    if (start == n.size()) {
        for (int num : n) cout << num << " ";
        cout << endl;
        return;
    }

    for (int i = start; i < n.size(); ++i) {
        // 剪枝：跳过重复数字
        /*bool duplicate = false;
        for (int j = start; j < i; ++j) {
            if (nums[j] == nums[i]) {
                duplicate = true;
                break;
            }
        }
        if (duplicate) continue;*/
        if(i > start && n[i] == n[i - 1]) continue;

        swap(n[start], n[i]);
        backtrack(n, start + 1);
        swap(n[start], n[i]);  // 回溯
    }
}

int main() {
    vector<int> n{5, 3, 3, 2};
    sort(n.begin(), n.end());
    backtrack(n);
    return 0;
}
